//
//  LoginViewController.m
//  Edutohome
//
//  Created by xiaofeifei on 15/1/13.
//
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetPwdViewController.h"

@interface LoginViewController (){
    BOOL _isRememberUser;
    BOOL _isAutomaticLogin;
}

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navStr = @"用户登录";
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 70, self.navView.frame.size.height-38 , 60, 30)];
    [rightBtn setTitle:@"注册" forState:UIControlStateNormal];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"back_n.png"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(gotoRegisterView:) forControlEvents:UIControlEventTouchUpInside];
    //    [rightBtn setImage:[UIImage imageNamed:@"nav_menu_icon.png"] forState:UIControlStateNormal];
    [self.navView addSubview:rightBtn];
    
    _isAutomaticLogin = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AutomaticLogin"] boolValue];
    _isRememberUser = [[[NSUserDefaults standardUserDefaults]objectForKey:@"RememberUser"] boolValue];
    if (_isRememberUser) {
        [_remBtn setImage:[UIImage imageNamed:@"select_s.png"]forState:UIControlStateNormal];
        _accountTextfield.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
        _pwdTextfield.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
    }else{
        [_remBtn setImage:[UIImage imageNamed:@"select_n.png"]forState:UIControlStateNormal];
    }
    if (_isAutomaticLogin) {
        [_autoLoginBtn setImage:[UIImage imageNamed:@"select_s.png"]forState:UIControlStateNormal];
    }else{
        [_autoLoginBtn setImage:[UIImage imageNamed:@"select_n.png"]forState:UIControlStateNormal];
    }
    // Do any additional setup after loading the view from its nib.
}



- (void)gotoRegisterView:(id)sender{
    RegisterViewController *vc = [[RegisterViewController alloc] initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma markv -IBAction
- (IBAction)gotoForgetPwd:(id)sender{
    ForgetPwdViewController *vc = [[ForgetPwdViewController alloc] initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)clickRememberUser:(id)sender{
    _isRememberUser = !_isRememberUser;
    if (_isRememberUser) {
        [_remBtn setImage:[UIImage imageNamed:@"select_s.png"]forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"RememberUser"];
    }else{
        [_remBtn setImage:[UIImage imageNamed:@"select_n.png"]forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"RememberUser"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"account"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
    }
}

- (IBAction)clickAutomaticLogin:(id)sender{
    _isAutomaticLogin = !_isAutomaticLogin;
    if (_isAutomaticLogin) {
        [_autoLoginBtn setImage:[UIImage imageNamed:@"select_s.png"]forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AutomaticLogin"];
    }else{
        [_autoLoginBtn setImage:[UIImage imageNamed:@"select_n.png"]forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"AutomaticLogin"];
    }
}

- (IBAction)loginView:(id)sender{
    if (_accountTextfield.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入账号"];
        return;
    }else if (_pwdTextfield.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入密码"];
        return;
    }
    [self getlogin];
}

#pragma mark -请求接口
- (void)getlogin{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parameters =[GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"user_name":_accountTextfield.text,@"password":_pwdTextfield.text}];
    
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@user/login?",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {

                                  [SVProgressHUD dismiss];
                                  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:nil];

                                  
                                  if ([result[@"UserID"] intValue] == -1) {
                                      [SVProgressHUD showInfoWithStatus:@"用户名不存在"];
                                  }
                                  else if ([result[@"UserID"] intValue] == 0) {
                                      [SVProgressHUD showInfoWithStatus:@"密码错误"];
                                  }
                                  else if ([result[@"UserID"] intValue] == -2) {
                                      [SVProgressHUD showInfoWithStatus:@"用户被禁用或被删除"];
                                  }
                                  else if ([result[@"UserID"] intValue] > 0){
                                      if (_isRememberUser||_isAutomaticLogin) {
                                          [[NSUserDefaults standardUserDefaults] setObject:_accountTextfield.text forKey:@"account"];
                                          [[NSUserDefaults standardUserDefaults] setObject:_pwdTextfield.text forKey:@"password"];
                                      }
                                      if (_isAutomaticLogin) {
                                          [[NSUserDefaults standardUserDefaults] setObject:jsonData forKey:@"userinfo"];
                                      }
                                      
                                      [[GlobalData sharedGlobalData] setUserinfoDic:result];
                                      [[GlobalData sharedGlobalData] getData];
                                      [[NSNotificationCenter defaultCenter] postNotificationName:@"AccountInfo" object:nil];
                                      [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                                      [self backBtnAct:nil];
                                  }
                              }];
}

#pragma mark -UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
